package com.blackhorse.product.controller;

import com.blackhorse.product.entity.Product;
import com.blackhorse.product.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

//Restful风格Controller
@RestController
@RequestMapping("/product")
public class ProductController {

    //获取本服务的ip和端口用于测试负载均衡
    @Value("${server.port}")
    private String port;
    @Value("${spring.cloud.client.ip-address}")
    private String ip;

    @Autowired
    private ProductService productService;

    @GetMapping("/{id}")
    public Product getById(@PathVariable Long id) {
        Product product = productService.findById(id);
        product.setProductName("ip:" + ip + ",port" + port);//将返回商品名设置为本服务ip和端口
        return product;
    }

    @PostMapping("")
    public String save(@RequestBody Product product) {//客户端提交json数据
        productService.save(product);
        return "保存成功";
    }

}
